using UnityEngine;	
using System.Collections;

public class WallBehaviour : MonoBehaviour{

	public Material[] materials;
	public float colorChangeInterval = 0.2F;
	private bool doesChangeColor = false;
	private bool doesRotation = false;
	public float rotationSpeed = 7f;

	public float scaleChangeFactor;
	public float scaleChangeInterval;

	public GameObject floor;

	private void Update()
	{
		// material change
		if (doesChangeColor)
		{
			int i = 0;
			int index = 0;
			foreach(Transform child in transform)
			{
				index = (int)(Time.time / colorChangeInterval);
				index = (index+i) % materials.Length;
				child.renderer.sharedMaterial = materials[index];
				i++;
			}
			index = (int)(Time.time / colorChangeInterval);
			index = (index) % materials.Length;
			//floor.renderer.sharedMaterial = materials[index];
		}
		if(doesRotation)
		{
			gameObject.transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime);
		}
	}

	public void SpawnWalls()
	{
		gameObject.rigidbody.useGravity = true;
		foreach(Transform child in transform)
		{
			child.GetComponentInChildren<MeshRenderer>().renderer.enabled = true;
		}
		StartCoroutine(DoPlayCrash());
	}

	public IEnumerator DoPlayCrash()
	{
		yield return new WaitForSeconds(0.3f);
		audio.Play();
	}

	public void ChangeColor()
	{
		doesChangeColor = true;
	}

	public void Rotate()
	{
		doesRotation = true;
	}

	public void ChangeScale()
	{
		StartCoroutine(DoChangeScale());
	}

	public IEnumerator DoChangeScale()
	{
		yield return new WaitForSeconds(1.5f);
		while(true)
		{
			iTween.ScaleTo(gameObject, transform.localScale*scaleChangeFactor, scaleChangeInterval);
			yield return new WaitForSeconds(scaleChangeInterval);
			iTween.ScaleTo(gameObject, transform.localScale/scaleChangeFactor, scaleChangeInterval);
			yield return new WaitForSeconds(scaleChangeInterval);
		}
	}
}